package ui.commodity;

import javax.swing.JPanel;

import tool.LongStringTransformer;
import tool.MyButton;
import vo.CommodityTypeVo;
import vo.CommodityVo;
import vo.UserVo;
import vo.Vo;
import blservice.commodity.CommodityBLService;
import blservice.commodity.CommodityTypeBLService;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JTable;

import client.boot.BLServiceManager;

import java.util.List;

public class CommodityManagementPanel extends JPanel {

	private static final long serialVersionUID = 8957255252153313577L;
	private CommodityBLService commodityBL;
	private UserVo user;
	private JTextField tfFind;
	private JTable table;
	private JScrollPane jsp;

	private Vector<String> columnNames;

	public CommodityManagementPanel(UserVo user) {

		try {
			commodityBL = BLServiceManager.getCommodityBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this, e1.getMessage());
			e1.printStackTrace();
		}

		this.user = user;
		this.setLayout(null);
		this.setOpaque(false);

		columnNames = new Vector<String>();
		
		columnNames.add("所属分类");
		columnNames.add("编号");
		columnNames.add("名称");
		columnNames.add("型号");
		columnNames.add("库存数量");
		columnNames.add("进价");
		columnNames.add("零售价");
		columnNames.add("最近进价");
		columnNames.add("最近零售价");
		columnNames.add("库存警戒值");
		/*
		 * commodityInfo.add("节能日光灯类"); commodityInfo.add("SP0001");
		 * commodityInfo.add("飞利浦日光灯"); commodityInfo.add("SR01");
		 * commodityInfo.add(0); commodityInfo.add(40L); commodityInfo.add(60L);
		 * commodityInfo.add(0); commodityInfo.add(0); commodityInfo.add(200);
		 * 
		 * rowData.add(commodityInfo);
		 */
		showAll();

		tfFind = new JTextField();
		tfFind.setBounds(444, 12, 151, 21);
		add(tfFind);
		tfFind.setColumns(10);

		JButton btnFind = new MyButton("Graphics/Button76-23.png");
		btnFind.setText("查找");
		btnFind.addActionListener(e -> findCommodity());
		btnFind.setBounds(610, 11, 76, 23);
		add(btnFind);

		JButton btnAdd = new MyButton("Graphics/Button93-23.png");
		btnAdd.setText("添加");
		btnAdd.addActionListener(e -> addCommodity());
		btnAdd.setBounds(86, 389, 93, 23);
		add(btnAdd);

		JButton btnDelete =new MyButton("Graphics/Button93-23.png");
		btnDelete.setText("删除");
		btnDelete.addActionListener(e -> deleteCommodity());
		btnDelete.setBounds(293, 389, 93, 23);
		add(btnDelete);

		JButton btnUpdate = new MyButton("Graphics/Button93-23.png");
		btnUpdate.setText("修改");
		btnUpdate.addActionListener(e -> updateCommodity());
		btnUpdate.setBounds(502, 389, 93, 23);
		add(btnUpdate);

	}

	private void addCommodity() {
		try {
			int row = table.getSelectedRow();
			String belongTo = (String) table.getValueAt(row, 0);
			String name = (String) table.getValueAt(row, 2);
			String type = (String) table.getValueAt(row, 3);
			int stocknum = 0;
			long importprice = 0;
			try {
				importprice = LongStringTransformer.StringToLong((String) table
						.getValueAt(row, 5));
			} catch (NumberFormatException e) {
				JOptionPane
						.showMessageDialog(this, "进价格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			long saleprice = 0;
			try {
				saleprice = LongStringTransformer.StringToLong((String) table
						.getValueAt(row, 6));
			} catch (NumberFormatException e) {
				JOptionPane
						.showMessageDialog(this, "售价格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;

			}
			int warningvalue = 0;

			try {
				warningvalue = Integer.parseInt((String) table.getValueAt(row,
						9));
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this,
						"库存警戒值格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;

			}
			CommodityVo addVo = new CommodityVo();
			addVo.setOperator(user.getName());
			addVo.setOperation("添加商品");
			addVo.setBelongTo(belongTo);
			addVo.setName(name);
			addVo.set型号(type);
			addVo.setstocknum(stocknum);
			addVo.set进价(importprice);
			addVo.set售价(saleprice);
			addVo.set库存数量警戒值(warningvalue);
			addVo.setOperation("增加商品");
			Vo vo = commodityBL.addCommodity(addVo);
			
			if (vo.isSuccessed()) {
				JOptionPane.showMessageDialog(this, "添加成功\n");
				showAll();
			} else {
				JOptionPane.showMessageDialog(this,
						"添加失败\n" + vo.getFailMessage());
			}

			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void deleteCommodity() {
		try {
			int row = table.getSelectedRow();

			String num = (String) table.getValueAt(row, 1);
			// System.out.println(name);
			CommodityVo deleteVo = new CommodityVo();
			deleteVo.setOperator(user.getName());
			deleteVo.setOperation("删除商品");
			deleteVo.set编号(num);
			Vo vo = commodityBL.deleteCommodity(deleteVo);
			if (vo.isSuccessed()) {
				JOptionPane.showMessageDialog(this, "删除成功\n");
				showAll();
			} else {
				JOptionPane.showMessageDialog(this,
						"删除失败\n" + vo.getFailMessage());
			}
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void updateCommodity() {
		try {
			int row = table.getSelectedRow();
			String belongTo = (String) table.getValueAt(row, 0);
			String identifier = (String) table.getValueAt(row, 1);
			String name = (String) table.getValueAt(row, 2);
			String type = (String) table.getValueAt(row, 3);
			int stocknum = 0;
			try {
				stocknum = Integer.parseInt((String) table.getValueAt(row, 4));
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this,
						"库存数量格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			long importprice = 0;
			try {
				importprice = LongStringTransformer.StringToLong((String) table
						.getValueAt(row, 5));
			} catch (NumberFormatException e) {
				JOptionPane
						.showMessageDialog(this, "进价格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			long recentimportprice = 0;
			try {
				importprice = LongStringTransformer.StringToLong((String) table
						.getValueAt(row, 7));
			} catch (NumberFormatException e) {
				JOptionPane
						.showMessageDialog(this, "进价格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			long recentsaleprice = 0;
			try {
				importprice = LongStringTransformer.StringToLong((String) table
						.getValueAt(row, 8));
			} catch (NumberFormatException e) {
				JOptionPane
						.showMessageDialog(this, "进价格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			long saleprice = 0;
			try {
				saleprice = LongStringTransformer.StringToLong((String) table
						.getValueAt(row, 6));
			} catch (NumberFormatException e) {
				JOptionPane
						.showMessageDialog(this, "售价格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;

			}
			int warningvalue = 0;

			try {
				warningvalue = Integer.parseInt((String) table.getValueAt(row,
						9));
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this,
						"库存警戒值格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;

			}
			CommodityVo updateVo = new CommodityVo();
			updateVo.setOperator(user.getName());
			updateVo.setOperation("修改商品");
			updateVo.setBelongTo(belongTo);
			updateVo.set编号(identifier);
			updateVo.setName(name);
			updateVo.set型号(type);
			updateVo.setstocknum(stocknum);
			updateVo.set进价(importprice);
			updateVo.set售价(saleprice);
			updateVo.set最近进价(recentimportprice);
			updateVo.set最近售价(recentsaleprice);
			updateVo.set库存数量警戒值(warningvalue);
			Vo vo = commodityBL.updateCommodity(updateVo);
			if (vo.isSuccessed()) {
				JOptionPane.showMessageDialog(this, "修改成功\n");
				showAll();
			} else {
				JOptionPane.showMessageDialog(this,
						"修改失败\n" + vo.getFailMessage());
			}
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void findCommodity() {
		try {
			CommodityVo findVo = new CommodityVo();
			Object keyword = tfFind.getText();
			findVo.setOperator(user.getName());
			findVo.setOperation("查找商品");
			findVo.setBelongTo(((String) keyword));
			findVo.set编号((String) keyword);
			findVo.setName((String) keyword);
			findVo.set型号(((String) keyword));
			Vector<Vector<String>> rowData = new Vector<Vector<String>>();
			Vector<String> commodityInfo;
			List<CommodityVo> commodityItem = commodityBL.findCommodity(findVo);
			for (int i = 0; i < commodityItem.size(); i++) {
				commodityInfo = new Vector<String>();
				commodityInfo.add(commodityItem.get(i).belongTo);
				commodityInfo.add(commodityItem.get(i).编号);
				commodityInfo.add(commodityItem.get(i).name);
				commodityInfo.add(commodityItem.get(i).型号);
				commodityInfo
						.add(Integer.toString(commodityItem.get(i).stocknum));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).进价));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).售价));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).最近进价));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).最近售价));
				commodityInfo.add(Integer.toString((commodityItem.get(i).库存数量警戒值)));
				rowData.add(commodityInfo);

			}
			commodityInfo = new Vector<String>();
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			rowData.add(commodityInfo);
			if (table != null) {
				table.setVisible(false);
			}
			table = new JTable(rowData, columnNames) {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int row, int column) {
				    if(column==1||column==4||column==7||column==8){
				    	return false;
				    }
				    else{
				    	return true;
				    }
				}
			};
			table.setVisible(true);
			table.setBounds(64, 51, 511, 319);
			if (jsp != null) {
				jsp.setVisible(false);
			}
			jsp = new JScrollPane(table);
			jsp.setBounds(0, 51, 686, 319);
			jsp.setVisible(true);
			this.add(jsp);

		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	public void showAll() {
		try {
			Vector<Vector<String>> rowData = new Vector<Vector<String>>();
			Vector<String> commodityInfo;
			List<CommodityVo> commodityItem = commodityBL.getAll();
			for (int i = 0; i < commodityItem.size(); i++) {
				commodityInfo = new Vector<String>();
				commodityInfo.add(commodityItem.get(i).belongTo);
				commodityInfo.add(commodityItem.get(i).编号);
				commodityInfo.add(commodityItem.get(i).name);
				commodityInfo.add(commodityItem.get(i).型号);
				commodityInfo
						.add(Integer.toString(commodityItem.get(i).stocknum));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).进价));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).售价));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).最近进价));
				commodityInfo.add(LongStringTransformer
						.LongToString(commodityItem.get(i).最近售价));
				commodityInfo
						.add(Integer.toString(commodityItem.get(i).库存数量警戒值));
				rowData.add(commodityInfo);

			}
			commodityInfo = new Vector<String>();
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			commodityInfo.add("");
			rowData.add(commodityInfo);
			if (table != null) {
				table.setVisible(false);
			}
			table = new JTable(rowData, columnNames){
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int row, int column) {
				    if(column==1||column==4||column==7||column==8){
				    	return false;
				    }
				    else{
				    	return true;
				    }
				}
			};
			table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JComboBox<String>(getLeafType())));
			table.setVisible(true);
			table.setBounds(64, 51, 511, 319);
			if (jsp != null) {
				jsp.setVisible(false);
			}
			jsp = new JScrollPane(table);
			jsp.setBounds(0, 51, 686, 319);
			jsp.setVisible(true);
			this.add(jsp);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}

	}
	
	private Vector<String> getLeafType(){
		Vector<String> result=new Vector<String>();
		try {
			CommodityTypeBLService bl=BLServiceManager.getCommodityTypeBLService(null);
			List<CommodityTypeVo> list=bl.getLeaf();
			for(CommodityTypeVo vo:list){
				result.add(vo.get名称());
			}
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

}